#include <mips/regdef.h>
#ifndef 	fp
#define	fp	$fp
#endif
#define	FACT_SSIZE	32
#define FACT_RA	24
#define	FACT_FP	20
#define	FACT_GP	16
#define	FACT_ARG0	FACT_SSIZE
	.align	2
	.global	factorial
	.ent factorial
factorial:
	.frame fp,FACT_SSIZE,ra
	.set noreorder
	.cpload	t9
	.set reorder
	subu	sp,sp,FACT_SSIZE
	sw	ra,FACT_RA(sp)
	sw	fp,FACT_FP(sp)
	sw	gp,FACT_GP(sp)
	move	fp,sp
	sw	a0,FACT_ARG0(fp)
	lw	a0,FACT_ARG0(fp)
	addiu	t1,zero,2
	blt	a0,t1,L0
	b	L1
L0:	addiu	v0,zero,1
	b	fin_factorial
L1:	subu	a0,a0,1
	la	t9,factorial
	jal t9
	lw	a0,FACT_ARG0(fp)
	mult	a0,v0
	mflo	v0
fin_factorial:
	move sp,fp
	lw	ra,FACT_RA(sp)
	lw	fp,FACT_FP(sp)
	lw	gp,FACT_GP(sp)
	lw	a0,FACT_ARG0(sp)
	addiu	sp,sp,FACT_SSIZE
	j ra
	.end factorial
